//
//  MessageDetailViewController.m
//  MIDI
//
//  Created by E.M.Game on 15/9/11.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "MessageDetailViewController.h"
#import "AppConfig.h"
#import "MyMessageTableCell.h"
#import "DelDataViewViewControl.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "UserDataSingleton.h"
#import "MyMessageModel.h"
#import "MJRefresh.h"
#import "CommFunc.h"
#import "MessageDetailCell.h"
#import "MessageDetailModel.h"

@interface MessageDetailViewController ()<UITableViewDelegate , UITableViewDataSource,
                                        DelDataViewDeletage, QXNetWorkToolDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) MessageDetailCell *cell;

@property (strong, nonatomic) MessageDetailModel *model;

@end

@implementation MessageDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self getData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.cell)
    {
        return [self.cell getHeight];
    }
    return 357;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.cell = [MessageDetailCell cellWithTableView:tableView];
    
    if (self.model)
    {
        self.cell.messageContent.text = [self getMessage:self.model.message_cotent];
        self.cell.dateLabel.text = self.model.message_time;
        self.cell.messageLabel.text = self.messageTitle;
    }
    
    return self.cell;
}

- (NSString *)getMessage:(NSString *)str
{
    NSString *message = str;
    
    NSRange range;
    
    range = [message rangeOfString:@"音乐基础"];
    if (range.location != NSNotFound)
    {
        NSRange rangeLeftRaw = [message rangeOfString:@"<"];
        NSRange rangeRightRaw = [message rangeOfString:@">"];
        
        if (rangeLeftRaw.location != NSNotFound &&
            rangeRightRaw.location != NSNotFound)
        {
            NSRange locRange;
            locRange.length = rangeRightRaw.location - rangeLeftRaw.location - 1;
            locRange.location = rangeLeftRaw.location + 1;
            
            NSString *replaeceMessage = @"音乐理论 ";
            NSRange levelRange;
            levelRange = [message rangeOfString:@"等级1"];
            if (levelRange.location != NSNotFound)
            {
                replaeceMessage = [replaeceMessage stringByAppendingString:@"初级"];
                
                message = [message stringByReplacingCharactersInRange:locRange withString:replaeceMessage];
                
                return message;
            }
            
            levelRange = [message rangeOfString:@"等级2"];
            if (levelRange.location != NSNotFound)
            {
                replaeceMessage = [replaeceMessage stringByAppendingString:@"中级"];
                
                message = [message stringByReplacingCharactersInRange:locRange withString:replaeceMessage];
                
                return message;
            }
            
            levelRange = [message rangeOfString:@"等级3"];
            if (levelRange.location != NSNotFound)
            {
                replaeceMessage = [replaeceMessage stringByAppendingString:@"高级"];
                
                message = [message stringByReplacingCharactersInRange:locRange withString:replaeceMessage];
                
                return message;
            }
        }
    }
    return message;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void) configNavigationItem
{
    [self.tabBarController configNavigationBarTintColor:UIColorRGBA(0, 0, 0, 1)];
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"消息详情";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],NSForegroundColorAttributeName,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],NSForegroundColorAttributeName,nil]];
    

    [self configNavigationBackAction:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(void)getData
{
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    
    QXNetWorkTool *tool = [[QXNetWorkTool alloc]init];
    
    tool.QXNetWorkDelegate = self;
    
    [tool GetDataFromServerWithPostMethodandInterfaceName:MessageDetailInterface andParameter:
     @[@{
           @"id":self.messageId}] tag:1];
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    [MBProgressHUD showError:message toView:self.view];
    
}

- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    NSString *mes = message;
    
    [MBProgressHUD showSuccess:mes toView:self.view];
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    switch (tag)
    {
        case 1:
        {
            for (NSDictionary *dic in Response)
            {
                self.model = [MessageDetailModel MessageDetailModelWithDict:dic];
                
                [self.tableView reloadData];
            }
            break;
        }
    }
}

	

@end



















